<?php
class User {
  private static $USER_XML_PATH = "data/user.xml";
  private static $instance;
  private $user_xml;
  private $user_node;
  
  private function __construct() {
    //Check Session
    if ($_SESSION["loggedin"] === true && Util::HasValue($_SESSION["username"])){
      //Verify login session
      $_SESSION["loggedin"] = false;
      $this->PrepareXml($_SESSION["username"]);
      if (isset($this->user_node) && $this->user_node->session == $_SESSION["session"]){
        $_SESSION["loggedin"] = true;
      }
    }
    //Check Cookie
    if ((!isset($_SESSION["loggedin"]) || $_SESSION["loggedin"] === false) && Util::HasValue($_COOKIE["username"]) && Util::HasValue($_COOKIE["session"])){
        $this->PrepareXml($_COOKIE["username"]);
        if (isset($this->user_node) && $this->user_node->session == $_COOKIE["session"]){
          $_SESSION["loggedin"] = true;
          $_SESSION["username"] = $_COOKIE["username"];
          $_SESSION["session"] = $_COOKIE["session"];
          $_SESSION["class"] = $_COOKIE["class"];
          $_SESSION["alpha"] = $_COOKIE["alpha"];
        }
    }
    
  }

  public static function GetUser() 
  {
      if (!isset(self::$instance)) {
          $c = __CLASS__;
          self::$instance = new $c;
      }
      return self::$instance;
  }
  
  private function PrepareXml($username=NULL){
    unset($this->user_xml);
    unset($this->user_node);
    $this->user_xml = simplexml_load_file(self::$USER_XML_PATH);
    if ($username != NULL){
      foreach ($this->user_xml->user as $u){
        if ($u->username == $username){
          $this->user_node = $u;
          break;
        }
      }
    }
  }
  
  private function TidyUp(){
    $tidy_options = array(
                'input-xml'    => true,
                'output-xml'   => true,
                'indent'       => true,
                'wrap'         => false,
        );

    $tidy = new tidy();
    $tidy->parseFile(self::$USER_XML_PATH, $tidy_options);
    $tidy->cleanRepair();
    $file = fopen (self::$USER_XML_PATH, "w"); 
    fwrite($file, $tidy); 
    fclose ($file);  
  }
  
  public function Login($username, $password, $remember = true){
    if (!Util::HasValue($username) || !Util::HasValue($password))
      return false;
    $password = md5($password);
    $this->PrepareXml($username);
    if (isset($this->user_node) && $this->user_node->password == $password){
      $guid = Util::GetGuid();
      $_SESSION["loggedin"] = true;
      $_SESSION["username"] = $username;
      $_SESSION["session"] = $guid;
      $_SESSION["class"] = $this->user_node->preferences->class."";
      $_SESSION["alpha"] = $this->user_node->preferences->alpha."";
      //write the session into xml
      $this->user_node->session = $guid;
      //$this->user_xml->asXML(self::$USER_XML_PATH);
      
      $this->SaveXml();
      
      //$this->TidyUp();  //May not be supported on server
      if ($remember){
          setcookie("username", $username, time()+259200000, "/");
          setcookie("session", $guid, time()+259200000, "/");
          setcookie("class", $_SESSION["class"], time()+259200000, "/");
          setcookie("alpha", $_SESSION["alpha"], time()+259200000, "/");
      }
      return true;
    }
    return false;
  }
  
  public function Logout(){
    setcookie("username", "", time()-259200000, "/");
    setcookie("session", "", time()-259200000, "/");
    setcookie("class", "", time()-259200000, "/");
    setcookie("alpha", "", time()-259200000, "/");
    unset($_SESSION["loggedin"]);
    unset($_SESSION["username"]);
    unset($_SESSION["session"]);
    unset($_SESSION["class"]);
    unset($_SESSION["alpha"]);
  }
  
  public function SavePreferences($class, $alpha){
    $this->PrepareXml($_SESSION["username"]);
    $this->user_node->preferences->class = $class;
    $this->user_node->preferences->alpha = $alpha;
    
    $_SESSION["class"] = $class;
    $_SESSION["alpha"] = $alpha;
    
    $this->SaveXml();
  }
  
  private function SaveXml(){
    $dom = new DOMDocument('1.0');
    $dom->preserveWhiteSpace = false;
    $dom->formatOutput = true;
    $dom->loadXML($this->user_xml->asXML(), LIBXML_NOBLANKS);
    $dom->save(self::$USER_XML_PATH);
  }
  
}
?>